package com.anssy.voteballot.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.anssy.voteballot.R;
import com.blankj.utilcode.util.ToastUtils;


public class UserInfoDialog extends Dialog {

    private Context context;
    private ClickListenerInterface clickListenerInterface;
    private EditText mEtInput;
    private Button mBtnCancel;
    private Button mBtnConfirm;
    private TextView mTvTitle;


    public interface ClickListenerInterface {
        void doConfirm(String code);

        void doCancel();

    }

    public UserInfoDialog(Context context) {
        super(context, R.style.my_dialog_share);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();
        initListener();
    }

    private void initView() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_userinfo, null);
        setContentView(view);
        mTvTitle = view.findViewById(R.id.tv_message_dialog);
        mEtInput = view.findViewById(R.id.et_input_userinfo);
        mBtnCancel = view.findViewById(R.id.btn_cancel_rename);
        mBtnConfirm = view.findViewById(R.id.btn_confirm_activation);
    }

    private void initData() {
        fillData();
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.9);
        dialogWindow.setAttributes(lp);
    }

    private void fillData() {
    }

    private void initListener() {
        mBtnCancel.setOnClickListener(new clickListener());
        mBtnConfirm.setOnClickListener(new clickListener());
    }


    public void setClickListener(ClickListenerInterface clickListenerInterface) {
        this.clickListenerInterface = clickListenerInterface;
    }

    private class clickListener implements View.OnClickListener {
        @SuppressLint("NonConstantResourceId")
        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {

                case R.id.btn_confirm_activation:
                    if (!TextUtils.isEmpty(mEtInput.getText().toString().trim())) {
                        clickListenerInterface.doConfirm(mEtInput.getText().toString().trim());
                    } else {
                        ToastUtils.showShort("请输入表决倒计时");
                    }
                    UserInfoDialog.this.dismiss();
                    break;


                case R.id.btn_cancel_rename:
                    clickListenerInterface.doCancel();
                    UserInfoDialog.this.dismiss();
                    break;
                default:
                    break;
            }
        }

    }
}
